package com.zhangzeqin.code.view;

import com.mysql.cj.xdevapi.Table;
import com.zhangzeqin.code.controller.AdminController;
import com.zhangzeqin.code.controller.WordBookController;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import java.sql.SQLException;

public class ListBookView extends JFrame {
    /**
     * 管理员控制层
     */
    AdminController adminController=new AdminController();
    WordBookController wordBookController=new WordBookController();
    /**
     * 表头
     */
    String[] columnNames= {"属性","单词书名" };
    /**
     * 表格所有行数据
     */
    Object[][] rowData=adminController.listBook();

    /**
     * 表格模型
     */
    private DefaultTableModel model;

    /**
     * 创建一个表格,指定表头和所有行数据
     */
    JTable table = new JTable(rowData, columnNames);

    /**
     *添加"增加","删除","修改"按钮
     */
    private JButton addButton=new JButton("增加");
    private JButton deleteButton=new JButton("删除");
    private JButton modifyButton=new JButton("返回");

    /**
     *添加文本框
     */
    private JTextField leftField=new JTextField(5);
    private JTextField rightField=new JTextField(8);
    private JTextField selectField=new JTextField(8);

    public static void main(String[] args) {
        new ListBookView();
    }
    public ListBookView(){
        super("单词书界面");
        //获取内容面板
        Container contentPane=getContentPane();
        setBounds(100,100,400,200);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        //表格修饰
        tableSetting();
        //设置模型
        model=new DefaultTableModel(rowData,columnNames);
        //引用模型
        table=new JTable(model);

        //设置滚动面板
        table.setPreferredScrollableViewportSize(new Dimension(400,300));
       //tablemodel:和jtable关联后,更新model就能把数据的变化反应到jtable
        DefaultTableModel tableModel=new DefaultTableModel();
        tableModel.setDataVector(rowData,columnNames);
        //jtable与table关联
        JTable jTable=new JTable(tableModel);
        //把表格放到滚动面板中(表头指定添加到滚动面板顶部)
        JScrollPane scrollPane=new JScrollPane(table);
        //添加滚动面板到内容面板
        contentPane.add(scrollPane);
        getContentPane().add(scrollPane,BorderLayout.CENTER);
        //调用按钮
        buttonInit();

        //监听器
        MyListener();


        //自定义图标
        URL imgUrl = LoginView.class.getClassLoader().getResource("image.png");
        setIconImage(new ImageIcon(imgUrl).getImage());

        //窗口大小
        setSize(600, 400);
        //关闭退出程序
        setLocationRelativeTo(null);
        setResizable(false);
        //可视
        setVisible(true);
    }

    private void MyListener() {
        //"添加"监听器
        addButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String rowData[]={leftField.getText(),rightField.getText()};
                //在表格模型中增加一行内容(文本框内容)
                if(leftField.getText().length()==0||rightField.getText().length()==0){
                    JOptionPane.showMessageDialog(null, "不能为空!");
                }else {
                    model.addRow(rowData);
                    wordBookController.addBook(rightField.getText(),leftField.getText());
                    JOptionPane.showMessageDialog(null, "添加成功!");
                }
            }
        });
        //"删除"监听器
        deleteButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
            //被选中的行数
                int selectRow = table.getSelectedRow();
                if (leftField.getText().length() == 0 ) {
                    JOptionPane.showMessageDialog(null, "请输入要删除书的属性");
                }
                if(selectRow==-1){
                    JOptionPane.showMessageDialog(null, "请选中改行");
                }
                else
                if(selectRow!=-1){
                    //删除选中的行
                    model.removeRow(selectRow);
                    try {
                        wordBookController.deleteBook(leftField.getText());
                    } catch (SQLException throwables) {
                        throwables.printStackTrace();
                    }
                    JOptionPane.showMessageDialog(null, "删除成功!");
                }
            }
        });
        //"返回"监听器
        modifyButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new AdminView();
            }
        });
    }

    private void buttonInit() {
        //默认流布局
        final JPanel centerpanel=new JPanel();
        getContentPane().add(centerpanel,BorderLayout.SOUTH);
        //将组件添加到面板
        centerpanel.add(new JLabel("搜索:"));
        centerpanel.add(selectField);
        centerpanel.add(new JLabel("属性:"));
        centerpanel.add(leftField);
        centerpanel.add(new JLabel("书名"));
        centerpanel.add(rightField);
        //增加按钮
        centerpanel.add(addButton);
        centerpanel.add(deleteButton);
        centerpanel.add(modifyButton);
    }

    private void tableSetting() {
        // 设置表格内容颜色
        // 字体颜色
        table.setForeground(Color.BLACK);
        // 字体样式
        table.setFont(new Font(null, Font.PLAIN, 14));
        // 选中后字体颜色
        table.setSelectionForeground(Color.DARK_GRAY);
        // 选中后字体背景
        table.setSelectionBackground(Color.LIGHT_GRAY);
        // 网格颜色
        table.setGridColor(Color.GRAY);

        //设置表头
        //表头名称字体样式
        table.getTableHeader().setFont(new Font(null,Font.BOLD,14));
        //表头名称字体颜色
        table.getTableHeader().setForeground(Color.RED);
        //设置不允许手动改变列宽
        table.getTableHeader().setReorderingAllowed(false);
        //设置不允许拖动重新排序各列
        table.getTableHeader().setReorderingAllowed(false);
        //设置行高
        table.setRowHeight(20);
        //第一列列宽
        table.getColumnModel().getColumn(0).setPreferredWidth(30);
    }
}
